package io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * Created by lizhenyu on 2019/10/26
 * description
 */
public class IOTest {

    private final String TEST_STRING = "Hello Java IO";

    private byte[] byteArray = TEST_STRING.getBytes().clone();
    private char[] charArray = TEST_STRING.toCharArray();

    /**
     * 字节流读取
     *
     * @return
     */
    public String readStrByStream() {
        ByteArrayInputStream is = new ByteArrayInputStream(byteArray);
        byte[] buffer;
        try {
            buffer = new byte[is.available()];
            if (is.read(buffer) != -1) {
                return new String(buffer, StandardCharsets.UTF_8);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 字节流写入
     *
     * @param str
     */
    public void writeStrByStream(String str) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            os.write(byteArray);
            os.write(str.getBytes());
            byteArray = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流读取
     *
     * @return
     */
    public String readString() {
        StringReader reader = new StringReader(String.valueOf(charArray));
        char[] buffer = new char[charArray.length];
        try {
            int len = reader.read(buffer, 0, buffer.length);
            if (len <= buffer.length) {
                return String.valueOf(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return null;
    }

    /**
     * 字符流写入
     *
     * @param str
     */
    public void writeString(String str) {
        StringWriter writer = new StringWriter();
        char[] buffer = str.toCharArray();
        try {
            writer.write(charArray);
            writer.write(buffer);
            charArray = writer.toString().toCharArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试IO流
     *
     * @param args
     */
    public static void main(String[] args) {
        IOTest test = new IOTest();
        System.out.println(test.readStrByStream());
        test.writeStrByStream(" 字节流");
        System.out.println(test.readStrByStream());

        System.out.println(test.readString());
        test.writeString(" 字符流");
        System.out.println(test.readString());
    }
}
